package com.ctshk.rpc.visa.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import com.ctshk.rpc.visa.dto.visa.VisaOrderReceiptDetailDTO;
import com.ctshk.rpc.visa.entity.*;
import com.ctshk.rpc.visa.mapper.*;
import com.ctshk.rpc.visa.req.visa.VisaOrderReceiptAddReq;
import com.ctshk.rpc.visa.req.visa.VisaOrderReceiptDetailReq;
import com.ctshk.rpc.visa.service.IVisaOrderReceiptService;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 签证订单-收据表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-26
 */
@DubboService
public class VisaOrderReceiptServiceImpl extends ServiceImpl<VisaOrderReceiptMapper, VisaOrderReceipt> implements IVisaOrderReceiptService {

    @Autowired
    private VisaOrderReceiptMapper visaOrderReceiptMapper;
    @Autowired
    private VisaOrderMapper visaOrderMapper;
    @DubboReference
    private ISysUserService iSysUserService;
    @Autowired
    VisaOrderTouristMapper visaOrderTouristMapper;
    @Autowired
    VisaProductMapper visaProductMapper;
    @Autowired
    VisaMapper visaMapper;
    @Autowired
    VisaTypeMapper visaTypeMapper;


    @Override
    public Result<VisaOrderReceiptDetailDTO> visaOrderReceiptDetail(VisaOrderReceiptDetailReq visaOrderReceiptDetailReq) {
        VisaOrderReceipt visaOrderReceipt = visaOrderReceiptMapper.selectById(visaOrderReceiptDetailReq.getOrderId());
        VisaOrderReceiptDetailDTO visaOrderReceiptDetailDTO = EntityUtil.copy(visaOrderReceipt, VisaOrderReceiptDetailDTO.class);
        return Result.success(visaOrderReceiptDetailDTO);
    }

    @Override
    public Result<Long> add(VisaOrderReceiptAddReq visaOrderReceiptAddReq, TokenUser tokenUser) {
        VisaOrder visaOrder = visaOrderMapper.selectById(visaOrderReceiptAddReq.getOrderId());
        if(visaOrder != null){
            VisaOrderReceipt visaOrderReceipt = new VisaOrderReceipt();
            visaOrderReceipt.setCreateId(tokenUser.getId());
            visaOrderReceipt.setRemark(visaOrder.getRemark());
//            visaOrderReceipt.setBusinessNumber(visaOrder.getOrderNumber());
            VisaProduct visaProduct =  visaProductMapper.selectById(visaOrder.getProductId());
            if(visaProduct != null){
                Visa visa = visaMapper.selectById(visaProduct.getVisaId());
                //待办证件服务
                QueryWrapper<VisaType> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("is_deleted",0);
                String handleAddressService = visa.getName();
                visaOrderReceipt.setHandleAddressService(handleAddressService);
                if(visa != null){
                    visaOrderReceipt.setNote(visa.getHandleInstruction());
                }
            }
            visaOrderReceipt.setGmtCreate(LocalDateTime.now());
            visaOrderReceipt.setOrderId(visaOrder.getId());
            //生成申请编号
            String applyNo = "H" + String.valueOf((int)(Math.random()*(999999-100000+1))+100000);
            visaOrderReceipt.setApplyNo(applyNo);
            visaOrderReceipt.setProcessingPlaceId(Long.parseLong(visaOrder.getProcessingPlaceId()));
            visaOrderReceipt.setProcessingPlace(visaOrder.getProcessingPlace());
            visaOrderReceipt.setProcessingDate(visaOrder.getProcessingDate());
            //門市編號+員工編號+年後2位+月日4位+A+時分秒6位+（S）
            Calendar cal = Calendar.getInstance();
            int hour=cal.get(Calendar.HOUR);//小时
            int minute=cal.get(Calendar.MINUTE);//分
            int second=cal.get(Calendar.SECOND);//秒
            String hourNew = String.valueOf(hour);
            String minuteNew = String.valueOf(minute);
            String secondNew = String.valueOf(second);
            if(hourNew.length() <2 ){
                hourNew = "0" + hourNew;
            }
            if(minuteNew.length() <2 ){
                minuteNew = "0" + minuteNew;
            }
            if(secondNew.length() <2 ){
                secondNew = "0" + secondNew;
            }
            String time =  hourNew + minuteNew + secondNew;
            UserDTO userDTO = iSysUserService.loadUserByParam(tokenUser.getId(), 1);
            String receiptNo = userDTO.getDept() + userDTO.getEmployeeNumber() + DateUtil.fmtNum(LocalDate.now()) + "A" + time + "(s)";
            visaOrderReceipt.setReceiptNo(receiptNo);
            visaOrderReceipt.setTakePlaceId(visaOrder.getTakePlaceId());
            visaOrderReceipt.setTakePlace(visaOrder.getTakePlace());
            visaOrderReceipt.setTakeDate(visaOrder.getTakeDate());
            QueryWrapper<VisaOrderTourist> queryWrapper = new QueryWrapper<VisaOrderTourist>();
            queryWrapper.eq("is_deleted",0);
            queryWrapper.eq("order_id",visaOrder.getId());
            List<VisaOrderTourist> visaOrderTouristList = visaOrderTouristMapper.selectList(queryWrapper);
            if(CollectionUtils.isNotEmpty(visaOrderTouristList)){
                VisaOrderTourist visaOrderTourist = visaOrderTouristList.get(0);
                visaOrderReceipt.setCertificatesNumber(visaOrderTourist.getCertificatesNumber());
                visaOrderReceipt.setName(visaOrderTourist.getName());
                UserDTO tourist = iSysUserService.loadUserByParam(visaOrderTourist.getTouristTypeId(), 1);
                visaOrderReceipt.setPhone(tourist.getMobile());

            }
        }
        return Result.success(visaOrder.getId());
    }
}
